package com.itchen.hospital.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itchen.hospital.Msg.BaseExceptionInfo;
import com.itchen.hospital.Msg.DefaultInfo;
import com.itchen.hospital.Msg.checkInfo;
import com.itchen.hospital.exception.CustomException;
import com.itchen.hospital.mapper.HospitalMapper;
import com.itchen.hospital.mapper.PatientAttendanceMapper;
import com.itchen.hospital.mapper.PatientMapper;
import com.itchen.hospital.pojo.*;
import com.itchen.hospital.service.PatientAttendanceService;
import com.itchen.hospital.service.PatientService;
import com.itchen.hospital.utils.JwtTokenUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class PatientAttendanceServiceImpl implements PatientAttendanceService {

    private final PatientAttendanceMapper patientAttendanceMapper;

    private final PatientMapper patientMapper;

    private final HospitalMapper hospitalMapper;

    //条件查询
    @Override
    public PageInfo<PatientAttendance> search(Params params) {
        //处理分页
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //获取登录人的姓名
        Admin currentUser = JwtTokenUtils.getCurrentUser();
        if (currentUser.getRole().equals(DefaultInfo.DefaultDoctorName)){
            params.setDoctorName(currentUser.getUsername());
        }
        if (currentUser.getRole().equals(DefaultInfo.DefaultUserName)){
            params.setPatientName(currentUser.getUsername());
        }
        List<PatientAttendance> list = patientAttendanceMapper.findBySearch(params);
        return PageInfo.of(list);
    }


    //修改
    @Override
    @Transactional
    public void update(Map<String, Object> map) {
        PatientAttendance patient = new PatientAttendance();
        patient.setId(Integer.parseInt(map.get("id").toString()));

        //医嘱不能为空
        if (map.get("doctorAdvice") == null){
            throw new CustomException(BaseExceptionInfo.OrdersNotNull);
        }
        patient.setDoctorAdvice(map.get("doctorAdvice").toString());
        patient.setInHospital(map.get("inHospital").toString());
        if (patient.getInHospital().equals("1")){
            patient.setCheckIn(checkInfo.yes);

            patientAttendanceMapper.update(patient);
            //在此处将信息存入住院登记表

            Hospital hospital = new Hospital();
            hospital.setPatientName((String) map.get("patientName"));
            hospital.setPatientAttendanceId((String) map.get("id"));
            hospital.setPatientAttendanceAppointmentTime((String) map.get("appointmentTime"));

            //根据此次添加信息查询住院登记表信息
            Hospital hospitalInfo = hospitalMapper.findByInfo(hospital);

            //判断此次存的信息是否已存在
            if (hospitalInfo != null){
                return;
            }
            hospitalMapper.insert(hospital);

        }else {
            patient.setCheckIn(checkInfo.no);
        }
        patientAttendanceMapper.update(patient);
    }

    //根据id查询
    @Override
    public PatientAttendance findById(Integer id) {
        PatientAttendance patient = patientAttendanceMapper.findById(id);
        return patient;
    }

}
